from jhplot import H2D
from jhplot import HPlotJa
from hep.aida import ITuple
from hep.aida import IHistogram2D
from jscinttools.utilities.aidautilities import AidaFactories
from jscinttools.utilities.aidautilities import AidaFileReader
from java.awt import Color
from java.awt import Font
from java.util import Locale

Locale.setDefault(Locale.ENGLISH) #For French OS

showFigures = True
writeFigures = not showFigures

aidaPath = "/home/marc-antoine/Documents/ZnWO4/Run_9/Reduced/TDC/2011_07_19/"
aidaFile = "Am241Cs137_3400mK_901_PhotonAnalysisConcat"
outputFile = "PSD"
win = [0, 400, 0, 0.3]
histo2D = AidaFactories.HISTOGRAM_FACTORY.createHistogram2D("Hsito 2D", 30, win[0], win[1], 30, win[2], win[3])

afr = AidaFileReader(aidaPath, aidaFile)
tup = afr.grabTuple("Total Tup")

tup.start()
for i in range(tup.rows()):
	tup.next()
	first1 = tup.getDouble(11)
	first2 = tup.getDouble(29)
	if (first1 > 199.99 and first1 < 203.0 and first2 > 199.99 and first2 < 203.0):
		nInf = tup.getInt(38) * 1.0
		nSh = tup.getInt(39) * 1.0
		numRatio = (nSh*1.0) / (nInf * 1.0)
		#if (nInf > win[0] and nInf < win[1] and numRatio > win[2] and numRatio > win[3]):
		histo2D.fill(nInf, numRatio)
		
h2d = H2D(histo2D)

"""
###############################################################
# Plotting Part
###############################################################
"""
""" Define colors and fonts"""
figType = [".png", ".eps", ".pdf"]
colors = [Color.blue, Color.red, Color(0, 170, 0), Color.black, Color.yellow]
axisLabelFont = Font(Font.SERIF, Font.PLAIN, 25)
titleFont = Font(Font.SERIF, Font.PLAIN, 25)
legendFont = Font(Font.SERIF, Font.PLAIN, 25)
ticFont = Font(Font.SERIF, Font.PLAIN, 20)
statBoxFont = Font(Font.SERIF, Font.PLAIN, 15)
prelimFont = Font(Font.SERIF, Font.BOLD, 30)
""" Create the Plotter"""
plotter = HPlotJa("PSD", 600, 600, 1, 1, True)
plotter.setGTitle("PSD", titleFont)
plotter.setAntiAlias(False)
plotter.setContour(True)
plotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
plotter.setRange(win[0], win[1], win[2], win[3])
plotter.setLogScale(0, False)
plotter.setLogScale(1, False)
plotter.setTicFont(ticFont)
plotter.setShowStatBox(False)
plotter.showKey(False)
plotter.setNameX("")
plotter.setNameY("")
if showFigures:
	plotter.visible(True)
else:
	plotter.visible(False)

""" Plots all the jhplot objects"""
plotter.draw(h2d)

#plotter.cd(1,1)
#theAxes_11 = plotter.getPad()
#theAxes_11.setLabelSpace(0, 0.0)
#theAxes_11.setLabelSpace(1, 0.0)
#HPlotJaUtil.setTickLabelZeroPositive(theAxes_11)
#
#
#""" Modifies the JaAxes and the axes labels"""
#labX = plotter.getLabelX()[0][0]
#labX.setFont(axisLabelFont)
#HPlotJaUtil.setLabelLocationAtCenter(0, labX, plotter, 0.025)
#labY = plotter.getLabelY()[0][0]
#labY.setFont(axisLabelFont)
#HPlotJaUtil.setLabelLocationAtCenter(1, labY, plotter, -0.01)
 

plotter.update()

""" Saves or not the plot"""
if writeFigures:
	for i in range(len(figType)):
		outFileName = outputFile + figType[i]
		plotter.export(outPath + File.separator + outFileName)
		print("Writting File : " + outPath + File.separator + outFileName)
		
print("That's all folks !")
if (not showFigures):
	exit()
